package com.study.java.jinjie.jihe.lianxi.one;

import java.util.List;

public class Score {
    private List<Student> studentList;

    public Score(List<Student> studentList) {
        this.studentList = studentList;
    }


    // 打印学生列表
    public void printStudent(){
        // 为了解决空指针异常先判断是否为空(null)
        if (studentList!=null){
            System.out.print("序号");
            System.out.print("\t 学号");
            System.out.print(" \t姓名");
            System.out.print(" \t成绩\n");

            // 打印分割线
            System.out.println("_________________________");

            // 遍历学生集合
            int length = studentList.size();  // 学生数量
            // 判断学生数量是否大于0
            if (length>0){
                // 根据学生长度使用for循环
                for (int i = 0; i < length; i++) {
                    // 从集合中获取学生
                    Student student = studentList.get(i);

                    // 解析学生信心 打印
                    System.out.print(i+1);  // 打印序号
                    System.out.print(" \t"+student.getNum());
                    System.out.print(" \t"+student.getName());
                    System.out.print(" \t"+student.getScore()+"\n");
                    // 打印分割线
                    System.out.println("----------------------------");
                }
            }
        }
    }


    // 平均成绩
    public int  pingJun() {
        int pingjun = 0;
        if (studentList != null && studentList.size() > 0) {
            int length = studentList.size();
            for (int i = 0; i < length; i++) {
                Student student = studentList.get(i);
                pingjun += student.getScore();
            }
            pingjun = pingjun / length;
        }
        return pingjun;
    }



    // 成绩最高的学生
    public String zuiGao(){

        int max = 0;
        String zui = " " ;
        if (studentList != null && studentList.size() > 0) {
            int length = studentList.size();
            for (int i = 0; i < length; i++) {
                Student student = studentList.get(i);
                if (student.getScore()>max) {
                    max = student.getScore();
                    zui = studentList.get(i).getName();
                }
            }
        }
        return zui;
    }


    public void printStudent2(){
        if (studentList!=null){
            System.out.println("序号"+" \t学号"+" \t姓名"+" \t成绩");
            System.out.println("----------------------------");

        int l = studentList.size();
        for (int i = 0; i < l; i++) {
            Student student = studentList.get(i);

            String c = c(student.getScore());
            System.out.print(i+1);
            System.out.print(" \t"+student.getNum());
            System.out.print(" \t"+student.getName());
            System.out.print(" \t"+c+"\n");
            System.out.println("----------------------------");
            }

        }

    }

    public void printStudent3(){
        if (studentList!=null){


            String title = String.format("%3s  %2s  %4s  %3s", "序号", "学号", "姓名", "成绩");
            System.out.println(title);
            System.out.println("----------------------------");

            int l = studentList.size();
            for (int i = 0; i < l; i++) {
                Student student = studentList.get(i);


                String row = String.format("%03d  %6d  %4s  %3d", i+1, student.getNum(), student.getName(), student.getScore());
                System.out.println(row);
                System.out.println("----------------------------");
            }

        }

    }
    public String c (short s){
        String c = " ";
        if (s >85){
            c = "优秀";
        } else if (s >75) {
            c = "良好";
        }else if (s >60) {
            c = "及格";
        } else if ( s >0) {
            c = "不及格";
        }else {
            c = "成绩无效";
        }
        return  c ;
    }
}
